package com.cy.aircompressor.main.welcome.fragment;

import android.os.Bundle;
import android.text.TextUtils;

import com.cy.aircompressor.config.AirConfig;
import com.cy.aircompressor.databinding.FragmentLoginBinding;
import com.cy.aircompressor.main.welcome.viewmodel.WelcomeViewModel;
import com.cy.aircompressor.utils.JumpPageUtils;
import com.drug.lib.util.SPManager;
import com.drug.lib.util.ToastUtil;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxCompoundButton;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.net.model.base.fragment.BaseVMFragment;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;

/**
 * 版权：周和权 版权所有
 *
 * @author zhouhequan
 * 版本：1.0
 * 创建日期：2023/4/4 or 14:17
 * 描述：进入app第一个展示页面
 */
public class LoginFragment extends BaseVMFragment<FragmentLoginBinding, WelcomeViewModel> {

    public static LoginFragment newInstance() {
        Bundle args = new Bundle();
        LoginFragment fragment = new LoginFragment();
        fragment.setArguments(args);
        return fragment;
    }



    @Override
    protected FragmentLoginBinding getViewBinding() {
        return FragmentLoginBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        Observable<CharSequence> name_observable = RxTextView.textChanges(mBinding.etUsername);
        Observable<CharSequence> pwd_observable = RxTextView.textChanges(mBinding.etPassword);
        Observable.combineLatest(name_observable, pwd_observable, new BiFunction<CharSequence, CharSequence, Boolean>() {
            @NonNull
            @Override
            public Boolean apply(@NonNull CharSequence charSequence, @NonNull CharSequence charSequence2) throws Exception {
                return !TextUtils.isEmpty(charSequence) && !TextUtils.isEmpty(charSequence2);
            }
        }).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                mBinding.btnLogin.setEnabled(aBoolean);
            }
        });

        RxView.clicks(mBinding.btnLogin)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe((o -> {
                    String name = mBinding.etUsername.getText().toString();
                    String pwd = mBinding.etPassword.getText().toString();
                    SPManager.putLoginInfo(name, pwd);
                    mViewModel.login(name, pwd);
                }));

        if (!TextUtils.isEmpty(SPManager.getUserName())) {
            mBinding.etUsername.setText(SPManager.getUserName());
        }
//        if (!TextUtils.isEmpty(SPManager.getPassWord())) {
//            mBinding.etPassword.setText(SPManager.getPassWord());
//        }
        mBinding.rememberPassword.setChecked(SPManager.getRememberPassword());


        RxCompoundButton.checkedChanges(mBinding.rememberPassword).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                SPManager.putRememberPassword(aBoolean.booleanValue());
            }
        });
//        UsageUtil.hasPermissionToReadNetworkStats(mContext);
    }

    @Override
    protected void requestNetwork() {

    }

    @Override
    protected void dataChange() {
        mViewModel.getLoginObservable().observe(this, boserver -> {
            boserver.handler(new OnCallback<String>() {
                @Override
                public void onSuccess(String data) {
                    SPManager.setToken(data);
                    AirConfig.get().updateLoginFlag(true);
                    JumpPageUtils.jumpMain();
                }

                @Override
                public void onFailure(Integer code, String message) {
                    super.onFailure(code, message);
                    ToastUtil.show(message);
                }
            });
        });
    }
}
